//
//  XJForgetPasswdViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJForgetPasswdViewController.h"

@interface XJForgetPasswdViewController ()<XJForgetPasswdViewDelegate>

@end

@implementation XJForgetPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"忘记密码";
    //self.navButtonRight.hidden = YES;
   // self.navButtonLeft.hidden = NO;
    [self.view addSubview:self.xjfpView];
    
    self.xjfpView.delegate = self;
    
}

- (XJForgetPasswdView *)xjfpView{
    
    if (!_xjfpView) {
        XJForgetPasswdView * xjfpView = [[XJForgetPasswdView alloc]initWithFrame:CGRectMake(0, XJTopNavH, KWidth, KHeight)];
        self.xjfpView = xjfpView;
    }
    return _xjfpView;
    
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//   // [self.navigationController setNavigationBarHidden:NO animated:YES];
//   // self.tabBarController.tabBar.hidden=YES;
//}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    
}

#pragma mark - XJForgetPasswdViewDelegate
- (void)fpClickLoginHandler {
    NSString *phone = self.xjfpView.fpPhoneTextField.text;
    NSString *code = self.xjfpView.fpCodeTextField.text;
    NSString *pwd = self.xjfpView.fpPasswordTextField.text;
    NSString *rePwd = self.xjfpView.fpRepasswordTextField.text;
    
    if ([XJTool isEmptyStr:phone]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    if ([XJTool isEmptyStr:code]) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    if ([XJTool isEmptyStr:pwd]) {
        [MBProgressHUD showError:@"请输入新密码"];
        return;
    }
    if (pwd.length<6 || pwd.length > 20) {
        [MBProgressHUD showError:@"密码长度6-20位"];
        return;
    }
    if ([XJTool isEmptyStr:code]) {
        [MBProgressHUD showError:@"请输入确认密码"];
        return;
    }
    if (![pwd isEqualToString:rePwd]) {
        [MBProgressHUD showError:@"两次密码不一致"];
        return;
    }
    
    // 密码MD5加密并转小写
    NSString *md5Str = [NSString md5:pwd];
    md5Str = md5Str.lowercaseString;
    
    NSDictionary *dic = @{@"phone":phone,
                          @"verify_code":code,
                          @"password":md5Str,
                          @"type":@"forget"
                          };
    [XJHttpTool requestForgetPwdWithDic:dic succesH:^(id responseObjc) {
        
        if ([responseObjc[@"code"] isEqualToNumber:@0]) {
           
            // 跳转 怎么跳转？
            [JRToast showWithText:@"密码修改成功"];
           
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [MBProgressHUD showError:responseObjc[@"message"]];
        }
    
    } failH:^(NSError *error) {
        XJLog(@"失败：%@",error);
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
